package LW.org.daoImple;

import LW.org.dao.BaseDao;
import LW.org.dao.roomUsedao;
import LW.org.model.Room;
import LW.org.model.RoomUse;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.Date;
import java.util.List;

public class roomUseImple extends BaseDao implements roomUsedao {

    //此处为退房的数据库操作API
    @Override
    public Boolean update_one(Date outtime, int bookId) {
        try {
            Session session = getSession();
            Query query = session.createQuery("update RoomUse r set r.hotelOutTime=:outtime, r.hotelOutflag=:flag where r.hotelInfoId=:bookId");
            query.setParameter("outtime", outtime);
            query.setParameter("flag", true);
            query.setParameter("bookId", bookId);
            int rows = query.executeUpdate();
            if (rows > 0) {
                System.out.println("更新 roomUse 成功。。。。。。");
                RoomUse one = session.get(RoomUse.class, bookId);
                Query qquery = session.createQuery("update Room o set o.roomStatus=:roomStatus where o.roomId=:roomId");
                qquery.setParameter("roomStatus", false);
                qquery.setParameter("roomId", one.getRoom().getRoomId());
                int rowss = qquery.executeUpdate();
                if (rowss > 0) {
                    System.out.println("退房后房间状态已更新");
                } else {
                    System.out.println("退房后房间状态更新失败");
                }
                return true;
            }
        } catch (Exception e) {
            System.out.println("roomUseDao Imple has error : " + e.toString());
        }
        return false;
    }

    @Override
    public Boolean delete_one(RoomUse delete_Id) {
        try {
            Session session = getSession();
            Query query = session.createQuery("delete RoomUse r where r.hotelInfoId=:Infoid");
            query.setParameter("Infoid", delete_Id);
            int rows = query.executeUpdate();
            if (rows > 0) {
                System.out.println("删除退房记录成功！！！");
                return true;
            }
        } catch (Exception e) {
            System.out.println("roomUseDao Imple's delete_one has error : " + e.toString());
        }
        return false;

    }

    @Override
    public List<RoomUse> select_all() {
        try {
            Session session = getSession();
            Query query = session.createQuery("from RoomUse r");
            List<RoomUse> all = query.list();
            if (all.size() > 0 && all != null) {
                return all;
            }
        } catch (Exception e) {
            System.out.println("select all RoomUse : " + e.toString());
        }
        return null;
    }

    @Override
    public List<RoomUse> select_key_ID(String IdCard, Boolean roomFlag) {
        try {
            Session session = getSession();
            Query query = session.createQuery("from RoomUse r where r.hotelClientIdcard=:IDcard and r.hotelOutflag=:hotelFlag");
            query.setParameter("hotelFlag", roomFlag);
            query.setParameter("IDcard", IdCard);
            List<RoomUse> all = query.list();
            if (all.size() > 0 && all != null) {
                return all;
            }
        } catch (Exception e) {
            System.out.println("select key_one_IDcard : " + e.toString());
        }
        return null;
    }

    @Override
    public List<RoomUse> select_key_roomID(int roomID, Boolean roomFlag) {
        try {
            Session session = getSession();
            Query query = session.createQuery("from RoomUse r where r.room.id=:roomID and r.hotelOutflag=:hotelFlag");
            query.setParameter("hotelFlag", roomFlag);
            query.setParameter("roomID", roomID);
            List<RoomUse> all = query.list();
            if (all.size() > 0 && all != null) {
                return all;
            }
        } catch (Exception e) {
            System.out.println("select key_one_for_RoomID : " + e.toString());
        }

        return null;
    }

    //此处为入住房间的操作API
    @Override
    public Boolean insert_one_use(RoomUse newInfo, int roomId) {
        try {
            Session session = getSession();
            SQLQuery squery = session.createSQLQuery("select count(*) from [hotel].[dbo].hotel_room_use");
            int oldCount = (int) squery.list().get(0);
            Room roomInfo = session.get(Room.class, roomId);
            RoomUse newUseinfo = new RoomUse();
            newUseinfo.setHotelClientName(newInfo.getHotelClientName());
            newUseinfo.setHotelClientIdcard(newInfo.getHotelClientIdcard());
            newUseinfo.setHotelClientTel(newInfo.getHotelClientTel());
            newUseinfo.setHotelInTime(newInfo.getHotelInTime());
            newUseinfo.setRoom(roomInfo);
            newUseinfo.setHotelOutflag(false);
            session.save(newUseinfo);
            int newCount = (int) squery.list().get(0);
            if (newCount == oldCount + 1) {
                Query query = session.createQuery("update Room o set o.roomStatus=:roomStatus where o.roomId=:roomId");
                query.setParameter("roomStatus", true);
                query.setParameter("roomId", roomId);
                int rows = query.executeUpdate();
                if (rows > 0) {
                    System.out.println("房间状态已更新");
                } else {
                    System.out.println("房间状态更新失败");
                }
            }
            return true;
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return false;
    }

    @Override
    @SuppressWarnings("unchecked")
    public Boolean update_roomUse(RoomUse newInfo, int oldRoomid) {
        try {
            Session session = getSession();
            Query query = session.createQuery("from RoomUse r where r.roomId=:roomId and r.hotelOutflag=:flag");
            query.setParameter("roomId", oldRoomid);
            query.setParameter("flag", false);
            List<RoomUse> roomuse = query.list();
            if (roomuse != null) {
                RoomUse one = roomuse.get(0);
                Room newRoom = session.get(Room.class, newInfo.getRoom().getRoomId());
                one.setRoom(newRoom);
                one.setHotelClientName(newInfo.getHotelClientName());
                one.setHotelClientIdcard(newInfo.getHotelClientIdcard());
                one.setHotelClientTel(newInfo.getHotelClientTel());
                one.setHotelInTime(newInfo.getHotelInTime());
                session.save(one);


            }
        } catch (Exception e) {
            System.out.println("update_roomUse has error : " + e.toString());
        }

        return false;
    }
//
//    public static Boolean updateRoomStatus(int roomId)throws Exception{
//
//        Session session =
//        Query query1 = session.createQuery("update Room o set o.roomStatus=:roomStatus where o.roomId=:roomId");
//        query1.setParameter("roomStatus", true);
//        query1.setParameter("roomId", roomId);
//        int rows = query1.executeUpdate();
//        if (rows > 0) {
//            System.out.println("房间状态已更新");
//            return true;
//        } else {
//            System.out.println("房间状态更新失败");
//        }
//        return false;
//
//    }
}
